<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>mainwindow.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-mainwindows-menus-example.html">Menus Example</a></td><td >mainwindow.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">mainwindow.cpp 示例文件</h1>
<span class="subtitle">mainwindows/menus/mainwindow.cpp</span>
<!-- $$$mainwindows/menus/mainwindow.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>

  <span class="preprocessor">#include &quot;mainwindow.h&quot;</span>

  MainWindow<span class="operator">::</span>MainWindow()
  {
      <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qwidget.html">QWidget</a></span>;
      setCentralWidget(widget);

      <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>topFiller <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qwidget.html">QWidget</a></span>;
      topFiller<span class="operator">-</span><span class="operator">&gt;</span>setSizePolicy(<span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding<span class="operator">,</span> <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding);

      infoLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;&lt;i&gt;Choose a menu option, or right-click to &quot;</span>
                                <span class="string">&quot;invoke a context menu&lt;/i&gt;&quot;</span>));
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setFrameStyle(<span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>StyledPanel <span class="operator">|</span> <span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>Sunken);
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setAlignment(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignCenter);

      <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>bottomFiller <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qwidget.html">QWidget</a></span>;
      bottomFiller<span class="operator">-</span><span class="operator">&gt;</span>setSizePolicy(<span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding<span class="operator">,</span> <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding);

      <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>;
      layout<span class="operator">-</span><span class="operator">&gt;</span>setMargin(<span class="number">5</span>);
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(topFiller);
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(infoLabel);
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(bottomFiller);
      widget<span class="operator">-</span><span class="operator">&gt;</span>setLayout(layout);

      createActions();
      createMenus();

      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> message <span class="operator">=</span> tr(<span class="string">&quot;A context menu is available by right-clicking&quot;</span>);
      statusBar()<span class="operator">-</span><span class="operator">&gt;</span>showMessage(message);

      setWindowTitle(tr(<span class="string">&quot;Menus&quot;</span>));
      setMinimumSize(<span class="number">160</span><span class="operator">,</span> <span class="number">160</span>);
      resize(<span class="number">480</span><span class="operator">,</span> <span class="number">320</span>);
  }

  <span class="preprocessor">#ifndef QT_NO_CONTEXTMENU</span>
  <span class="type">void</span> MainWindow<span class="operator">::</span>contextMenuEvent(<span class="type"><a href="../qtgui/qcontextmenuevent.html">QContextMenuEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="type"><a href="qmenu.html">QMenu</a></span> menu(<span class="keyword">this</span>);
      menu<span class="operator">.</span>addAction(cutAct);
      menu<span class="operator">.</span>addAction(copyAct);
      menu<span class="operator">.</span>addAction(pasteAct);
      menu<span class="operator">.</span>exec(event<span class="operator">-</span><span class="operator">&gt;</span>globalPos());
  }
  <span class="preprocessor">#endif // QT_NO_CONTEXTMENU</span>

  <span class="type">void</span> MainWindow<span class="operator">::</span>newFile()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;File|New&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>open()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;File|Open&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>save()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;File|Save&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>print()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;File|Print&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>undo()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Undo&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>redo()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Redo&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>cut()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Cut&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>copy()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Copy&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>paste()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Paste&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>bold()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Format|Bold&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>italic()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Format|Italic&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>leftAlign()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Format|Left Align&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>rightAlign()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Format|Right Align&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>justify()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Format|Justify&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>center()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Format|Center&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setLineSpacing()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Format|Set Line Spacing&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setParagraphSpacing()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Edit|Format|Set Paragraph Spacing&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>about()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Help|About&lt;/b&gt;&quot;</span>));
      <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>about(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;About Menu&quot;</span>)<span class="operator">,</span>
              tr(<span class="string">&quot;The &lt;b&gt;Menu&lt;/b&gt; example shows how to create &quot;</span>
                 <span class="string">&quot;menu-bar menus and context menus.&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>aboutQt()
  {
      infoLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Invoked &lt;b&gt;Help|About Qt&lt;/b&gt;&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>createActions()
  {
      newAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;New&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      newAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>New);
      newAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Create a new file&quot;</span>));
      connect(newAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>newFile);

      openAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Open...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      openAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Open);
      openAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Open an existing file&quot;</span>));
      connect(openAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>open);

      saveAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Save&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      saveAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Save);
      saveAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Save the document to disk&quot;</span>));
      connect(saveAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>save);

      printAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Print...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      printAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Print);
      printAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Print the document&quot;</span>));
      connect(printAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>print);

      exitAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;E&amp;xit&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      exitAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Quit);
      exitAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Exit the application&quot;</span>));
      connect(exitAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>close);

      undoAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Undo&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      undoAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Undo);
      undoAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Undo the last operation&quot;</span>));
      connect(undoAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>undo);

      redoAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Redo&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      redoAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Redo);
      redoAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Redo the last operation&quot;</span>));
      connect(redoAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>redo);

      cutAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;Cu&amp;t&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      cutAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Cut);
      cutAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Cut the current selection's contents to the &quot;</span>
                              <span class="string">&quot;clipboard&quot;</span>));
      connect(cutAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>cut);

      copyAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Copy&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      copyAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Copy);
      copyAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Copy the current selection's contents to the &quot;</span>
                               <span class="string">&quot;clipboard&quot;</span>));
      connect(copyAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>copy);

      pasteAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Paste&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      pasteAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Paste);
      pasteAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Paste the clipboard's contents into the current &quot;</span>
                                <span class="string">&quot;selection&quot;</span>));
      connect(pasteAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>paste);

      boldAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Bold&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      boldAct<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      boldAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Bold);
      boldAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Make the text bold&quot;</span>));
      connect(boldAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>bold);

      <span class="type"><a href="../qtgui/qfont.html">QFont</a></span> boldFont <span class="operator">=</span> boldAct<span class="operator">-</span><span class="operator">&gt;</span>font();
      boldFont<span class="operator">.</span>setBold(<span class="keyword">true</span>);
      boldAct<span class="operator">-</span><span class="operator">&gt;</span>setFont(boldFont);

      italicAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Italic&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      italicAct<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      italicAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Italic);
      italicAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Make the text italic&quot;</span>));
      connect(italicAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>italic);

      <span class="type"><a href="../qtgui/qfont.html">QFont</a></span> italicFont <span class="operator">=</span> italicAct<span class="operator">-</span><span class="operator">&gt;</span>font();
      italicFont<span class="operator">.</span>setItalic(<span class="keyword">true</span>);
      italicAct<span class="operator">-</span><span class="operator">&gt;</span>setFont(italicFont);

      setLineSpacingAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;Set &amp;Line Spacing...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      setLineSpacingAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Change the gap between the lines of a &quot;</span>
                                         <span class="string">&quot;paragraph&quot;</span>));
      connect(setLineSpacingAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>setLineSpacing);

      setParagraphSpacingAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;Set &amp;Paragraph Spacing...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      setParagraphSpacingAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Change the gap between paragraphs&quot;</span>));
      connect(setParagraphSpacingAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>setParagraphSpacing);

      aboutAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;About&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      aboutAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Show the application's About box&quot;</span>));
      connect(aboutAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>about);

      aboutQtAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;About &amp;Qt&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      aboutQtAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Show the Qt library's About box&quot;</span>));
      connect(aboutQtAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <a href="qapplication.html#qApp">qApp</a><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>aboutQt);
      connect(aboutQtAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>aboutQt);

      leftAlignAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Left Align&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      leftAlignAct<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      leftAlignAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+L&quot;</span>));
      leftAlignAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Left align the selected text&quot;</span>));
      connect(leftAlignAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>leftAlign);

      rightAlignAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Right Align&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      rightAlignAct<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      rightAlignAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+R&quot;</span>));
      rightAlignAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Right align the selected text&quot;</span>));
      connect(rightAlignAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>rightAlign);

      justifyAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Justify&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      justifyAct<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      justifyAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+J&quot;</span>));
      justifyAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Justify the selected text&quot;</span>));
      connect(justifyAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>justify);

      centerAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">&quot;&amp;Center&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      centerAct<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      centerAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+E&quot;</span>));
      centerAct<span class="operator">-</span><span class="operator">&gt;</span>setStatusTip(tr(<span class="string">&quot;Center the selected text&quot;</span>));
      connect(centerAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>center);

      alignmentGroup <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qactiongroup.html">QActionGroup</a></span>(<span class="keyword">this</span>);
      alignmentGroup<span class="operator">-</span><span class="operator">&gt;</span>addAction(leftAlignAct);
      alignmentGroup<span class="operator">-</span><span class="operator">&gt;</span>addAction(rightAlignAct);
      alignmentGroup<span class="operator">-</span><span class="operator">&gt;</span>addAction(justifyAct);
      alignmentGroup<span class="operator">-</span><span class="operator">&gt;</span>addAction(centerAct);
      leftAlignAct<span class="operator">-</span><span class="operator">&gt;</span>setChecked(<span class="keyword">true</span>);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>createMenus()
  {
      fileMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;File&quot;</span>));
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(newAct);
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(openAct);
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(saveAct);
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(printAct);
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();
      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(exitAct);

      editMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Edit&quot;</span>));
      editMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(undoAct);
      editMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(redoAct);
      editMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();
      editMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(cutAct);
      editMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(copyAct);
      editMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(pasteAct);
      editMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();

      helpMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Help&quot;</span>));
      helpMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(aboutAct);
      helpMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(aboutQtAct);

      formatMenu <span class="operator">=</span> editMenu<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Format&quot;</span>));
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(boldAct);
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(italicAct);
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator()<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Alignment&quot;</span>));
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(leftAlignAct);
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(rightAlignAct);
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(justifyAct);
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(centerAct);
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(setLineSpacingAct);
      formatMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(setParagraphSpacingAct);
  }

</pre>
</div>
<!-- @@@mainwindows/menus/mainwindow.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
